package com.xhwl.centiotdata.pollingtrigger.vehicle;

import com.xhwl.centiotdata.handler.vehicle.IVehicleSync;
import com.xhwl.centiotdata.pojo.enums.vehicle.VehicleSyncEnum;
import com.xhwl.centiotdata.pollingtrigger.ScheduleDispatcher;
import com.xhwl.centiotdata.service.integrateconfig.VehicleIntegrationConfig;
import com.xhwl.common.pojo.cent.vehicle.bo.config.VehicleIntegrationConfigBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class VehicleSyncDispatcher implements ScheduleDispatcher {

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private VehicleIntegrationConfig vehicleIntegrationConfig;

    @Override
    public void doOneHourTask() {
        Map<Integer, List<VehicleIntegrationConfigBO>> integrationConfigs = vehicleIntegrationConfig.getIntegrationConfigs();

        integrationConfigs.forEach((key, value) -> {
            if (1==value.size()) {
                VehicleIntegrationConfigBO vehicleIntegrationConfigBO = value.get(0);

                Integer productId = vehicleIntegrationConfigBO.getVehicleSystemInfoBO().getProductId();
                IVehicleSync vehicleSync = applicationContext.getBean(VehicleSyncEnum.toVehicleSyncEnum(productId).getSyncServiceName(), IVehicleSync.class);

                vehicleSync.sync(vehicleIntegrationConfigBO);
            } else {
                log.error("该项目具有多个集成配置，因此该项目不会做任何逻辑行为，项目id：{}", key);
            }
        });

    }

}
